package com.hqyj.aop;

import com.hqyj.exception.ParamValidException;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;


/**
 * @Description
 * @Autor 伍军
 * @Date 2022/6/23 15:55
 * @Version 1.0
 **/
@Aspect
@Component
public class ParamValideAop {
    //定义校验的包位置
    @Pointcut("execution(* com.hqyj.controller..*(..))")
    public void pointcut() {
    }

    /**
     * 定义处理参数校验的通知
     * @param joinPoint
     * @param bindingResult
     * @throws ParamValidException
     */
   @Before("pointcut() && args(..,bindingResult)")
    public void doBefore(JoinPoint joinPoint,BindingResult bindingResult) throws ParamValidException {

        //当校验不通过时抛出自定义参数校验异常
        if(bindingResult != null && bindingResult.hasErrors()){

            throw new ParamValidException(bindingResult);
        }
    }

}